package com.nexgo.payment.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nexgo.payment.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaox on 2016/12/12.
 */

public class ListMenuAdapter extends RecyclerView.Adapter<ListMenuAdapter.ViewHolder> {
    private List<HashMap<String, Integer>> mCurrentItems;
    private OnItemClickListener mListener;

    public ListMenuAdapter() {
        mCurrentItems = new ArrayList<>();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public void addItem(int stringId, int drawableId) {
        HashMap<String, Integer> item = new HashMap<>();
        item.put("text", stringId);
        if (drawableId != 0) {
            item.put("image", drawableId);
        }
        if (!isItemExisted(item)) {
            mCurrentItems.add(item);
            notifyItemInserted(getItemCount());
        }
    }

    private boolean isItemExisted(HashMap<String, Integer> item) {
        for (HashMap<String, Integer> aItem : mCurrentItems) {
            if (aItem.get("text").equals(item.get("text"))
                    && aItem.get("image").equals(item.get("image"))) {
                return true;
            }
        }
        return false;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_menu_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (mCurrentItems.size() > 0) {
            holder.itemText.setText(mCurrentItems.get(position).get("text"));
            if (mCurrentItems.get(position).containsKey("image")) {
                holder.itemImage.setVisibility(View.VISIBLE);
                holder.itemImage.setImageResource(mCurrentItems.get(position).get("image"));
            }
            holder.itemView.setTag(position);
        }
    }

    @Override
    public int getItemCount() {
        return mCurrentItems.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.item_image)
        ImageView itemImage;
        @BindView(R.id.item_text)
        TextView itemText;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mListener != null) {
                mListener.onClick((Integer) view.getTag());
            }
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }
}
